package org.qydjk.console.controller.product;

import com.sun.org.apache.regexp.internal.RE;
import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.product.ProductSpecification;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.console.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 产品规格
 * Created by Edward_yang on 2017/12/26 0026.
 */
@Controller
@RequestMapping("/productSpecification")
public class ProductSpecificationController extends BaseController{
    @Autowired
    private IProductService productService;
    /***
     * 产品规格
     * 跳转
     * @author Edward_yang
     * @uptime 2017-12-26
     * @return
     */
    @RequestMapping("/gotoProductSpecification.do")
    public String  gotoProductSpecification(){
        return "/product/productSpecificationInfo";
    }

    /***
     * 产品列表查询
     * @author Edward_yang
     * @uptime 2017-12-26
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/productList.do")
    @ResponseBody
    public Object productList(Integer page,Integer rows,String keyWord){
        try {
            return productService.findProductAllList(page,rows,keyWord);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /***
     * 产品规格列表查询
     * @author Edward_yang
     * @uptime 2017-12-26
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/productSpecificationList.do")
    @ResponseBody
    public Object productSpecificationList(Integer page,Integer rows,String productId){
        HibernatePage pager = new HibernatePage(page,rows);
        String sql="SELECT qps.id as id,qps.name as name,qps.logo as logo,qps.price as price,qps.product_id as productId ,qps.sold_out_count as soldOutCount, qps.repertory as repertory ,qps.create_time as createTime,qps.ord as ord  " +
                " FROM  qy_product_specification qps" +
                " where 1=1";
        String countSql="SELECT count(qps.id) "+
                "  FROM  qy_product_specification qps where 1=1 " ;
        if(!StringUtil.isEmptyOrNull(productId)){
            sql+=" and qps.product_id="+productId;
            countSql+=" and qps.product_id="+productId;
        }
        sql+= " ORDER BY  qps.create_time desc";
        try{
            List<Map<String,Object>> productSpecificationList=baseService.findMapBySQL(sql,pager);
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total,productSpecificationList);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /***
     * 规格添加
     * @author Edward_yang
     * @uptime 2017-12-26
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public Object addAttribute(ProductSpecification productSpecification){
        try {
            productSpecification.setCreateTime(new Date());//创建时间
            productSpecification.setUpdateTime(new Date());
            baseService.save(productSpecification);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /***
     * 规格修改
     * @author Edward_yang
     * @uptime 2017-12-26
     */
    @RequestMapping("/edit.do")
    @ResponseBody
    public Object editAttribute(ProductSpecification productSpecification){
        try {
            ProductSpecification productSpecification11=baseService.get(ProductSpecification.class,productSpecification.getId());
            BeanUtil.copyProperties(productSpecification,productSpecification11);
            productSpecification11.setUpdateTime(new Date());//修改时间
            baseService.saveOrUpdate(productSpecification11);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /***
     * 规格删除
     * @author Edward_yang
     * @uptime 2017-12-26
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Object deleteAttribute(Integer id){
        try {
            ProductSpecification productSpecification=baseService.get(ProductSpecification.class,id);
            baseService.delete(productSpecification);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

}
